package com.tilaiedu.mvc.controller;

import com.tilaiedu.mvc.domain.User;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @Author xlzhang
 * @Date 2022/5/24 9:47
 * @Version 1.0
 * @Description TODO
 */
@Controller
// @SessionAttributes("msg")
public class UserController {

    @GetMapping("/success.html")
    public String openSuccess(){
        return "success";
    }

    @GetMapping("/api")
    public String testApi(HttpServletRequest request,
                          HttpServletResponse response,
                          HttpSession session,
                          Model model,
                          ModelMap modelMap) throws IOException, ServletException {

        System.out.println("request = " + request);
        System.out.println("response = " + response);
        System.out.println("session = " + session);


        //session.setAttribute("msg","session");
        // 只使用于请求转发时携带数据
        //request.setAttribute("msg","asdfas");
        //model.addAttribute("msg","我是通过model绑定的数据");
        modelMap.addAttribute("msg","我是通过model绑定的数据map");
        //request.getRequestDispatcher("success.html").forward(request,response);
        //response.sendRedirect(request.getContextPath()+"/success.html");
        //response.setContentType("text/html;charset=utf-8");
        //response.getWriter().write("woshi 蔡徐坤");
        // 页面的跳转就是请求转发
        return "success";
    }

    // 如果参数类型是集合类型，需要加上注解@RequestParam
    // Map无法封装内部列表数据，只能获取第一个
    @RequestMapping("/userMap")
    public ModelAndView getUser2(@RequestParam Map user, @RequestParam List<String> likes) throws Exception {
        // 请求参数的编码设置，这句代码必须在servlet获取请求参数之前
        // request.setCharacterEncoding("utf-8");
        ModelAndView mv = new ModelAndView();
        System.out.println("-----UserController.getUser");
        // 设置的逻辑试图
        mv.setViewName("success");
        mv.addObject("msg",user);

        System.out.println("user = " + user);
        System.out.println("likes = " + likes);
        return mv;
    }

    // 如果参数类型是集合类型，需要加上注解@RequestParam
    @RequestMapping("/list")
    public ModelAndView getUser2(@RequestParam List<String> likes) throws Exception {
        // 请求参数的编码设置，这句代码必须在servlet获取请求参数之前
        // request.setCharacterEncoding("utf-8");
        ModelAndView mv = new ModelAndView();
        System.out.println("-----UserController.getUser");
        // 设置的逻辑试图
        mv.setViewName("success");
        // 默认是添加到request对象中  （请求域中）
        mv.addObject("msg",likes);

        System.out.println("likes = " + likes);
        return mv;
    }

    // springmvc可以自动将请求参数封装到对象中
    @RequestMapping("/userObj")
    public ModelAndView getUser2(User user) throws Exception {
        // 请求参数的编码设置，这句代码必须在servlet获取请求参数之前
       // request.setCharacterEncoding("utf-8");
        ModelAndView mv = new ModelAndView();
        System.out.println("-----UserController.getUser");
        // 设置的逻辑试图
        mv.setViewName("success");
        mv.addObject("msg",user.toString());

        System.out.println("user = " + user);
        return mv;
    }

    @RequestMapping("/user")
    public ModelAndView getUser(ModelAndView mv,
                                @RequestParam(value = "name",required = false) String name,
                                @RequestParam(defaultValue = "18") Integer age,
                                Double height,
                                @RequestHeader(value = "User-Agent",required = false) String userAgent,
                                @CookieValue("JSESSIONID") String jsessionid){

        System.out.println("-----UserController.getUser");
        System.out.println("userAgent = " + userAgent);
        System.out.println("jsessionid = " + jsessionid);
        // 设置的逻辑试图
        mv.setViewName("success");
        mv.addObject("msg",name +"-"+ age +"-"+ height);
        return mv;
    }
}
